//
//  WantPeopleViewController.m
//  Liuhe
//
//  Created by zr on 2018/8/7.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "WantPeopleViewController.h"
#import "JobTableViewCell.h"
#import "JobInfoViewController.h"
#import "PushJobNeedsViewController.h"
#import "HomeJobListModel.h"

@interface WantPeopleViewController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITextField * searchTextfiled;
@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray<HomeJobListModel *> * jobListArray;

@end

@implementation WantPeopleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = WHITE_COLOR;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToJobInfo) name:@"pushToJobInfo" object:nil];
    
    [HomeJobListModel homeJobWithDict:nil WithSucess:^(NSArray<HomeJobListModel *> *array) {
        
        self.jobListArray = array;
    } error:^{
        
    }];
    
    [self setupNav];
    [self setupUI];
    
}


- (void)setJobListArray:(NSArray<HomeJobListModel *> *)jobListArray{
    
    _jobListArray = jobListArray;
    
    [self.tableView reloadData];
}


- (void)setupNav{
    
    self.title = @"";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(0, 0, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
    
    UIButton * backButton1 = [[UIButton alloc] init];
    
    //    backButton.frame = CGRectMake(0 , 0 , 65 * SCREENH6, 20 * SCREENH6);
    [backButton1 setTitle:@"发布" forState:UIControlStateNormal];
    backButton1.titleLabel.font = [UIFont boldSystemFontOfSize:15 * SCREENH6];
    [backButton1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [backButton1 addTarget:self action:@selector(ClickPushButton) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * nextButton = [[UIBarButtonItem alloc] initWithCustomView:backButton1];
    
    self.navigationItem.rightBarButtonItem = nextButton;
    
    
}

- (void)backToView{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)setupUI{
    
    UITextField * searchTextfiled = [[UITextField alloc] initWithFrame:CGRectMake(0, 5 * SCREENH6, 260 * SCREENH6, 30 * SCREENH6)];
    
    searchTextfiled.borderStyle = UITextBorderStyleRoundedRect;
    searchTextfiled.backgroundColor = [UIColor whiteColor];
    //设置光标颜色
    searchTextfiled.tintColor = [UIColor whiteColor];
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0xbebebe);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    searchTextfiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入职位/公司名称关键词" attributes:attrs];
    searchTextfiled.textColor = [UIColor blackColor];
    searchTextfiled.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
    //设置显示模式为永远显示(默认不显示)
    searchTextfiled.leftViewMode = UITextFieldViewModeAlways;
    searchTextfiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    UIView * rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 35 * SCREENH6, 20 * SCREENH6)];
    UIImageView *image=[[UIImageView alloc] initWithFrame:CGRectMake(5, 0, 16 * SCREENH6, 17.5 * SCREENH6)];//WithImage:[UIImage imageNamed:@"手机号"]];
    image.image = ImageNamed(@"hg");
    [rightView addSubview:image];
    searchTextfiled.leftView = rightView;
    searchTextfiled.leftViewMode = UITextFieldViewModeAlways;
    searchTextfiled.returnKeyType = UIReturnKeySearch;
    searchTextfiled.delegate = self;
    
    self.searchTextfiled = searchTextfiled;
    
    //    [view addSubview:self.searchTextfiled];
    
    self.navigationItem.titleView = self.searchTextfiled;
    
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.rowHeight = 122 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[JobTableViewCell class] forCellReuseIdentifier:@"JobListCell"];
    
    [self.view addSubview:self.tableView];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.jobListArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JobTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JobListCell" forIndexPath:indexPath];

    cell.model = self.jobListArray[indexPath.row];

    return cell;
    
}


- (void)ClickPushButton{
    
    HIDDEN_TAB;

    PushJobNeedsViewController * pushVC = [PushJobNeedsViewController new];

    [self.navigationController pushViewController:pushVC animated:YES];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    HIDDEN_TAB;
    
    JobInfoViewController * jobinfoVC = [JobInfoViewController new];
    
    HomeJobListModel * model = self.jobListArray[indexPath.row];
    jobinfoVC.model = model;
    
    [self.navigationController pushViewController:jobinfoVC animated:YES];
}


//实现UITextField代理方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];//取消第一响应者
    
    NSDictionary * dict = @{@"position":textField.text};
    
    [HomeJobListModel homeJobWithDict:dict WithSucess:^(NSArray<HomeJobListModel *> *array) {
        
        self.jobListArray = array;
    } error:^{
        
    }];
    
    return YES;
}


- (void)pushToJobInfo{
    
//    HIDDEN_TAB;
//
//    JobInfoViewController * jobinfoVC = [JobInfoViewController new];
//
//    [self.navigationController pushViewController:jobinfoVC animated:YES];
}


- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
